﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WuDada.Core.Post;
using WuDada.Core.Post.Service;
using WuDada.Core.Post.Domain;
using Common.Logging;
using WuDada.Core.SystemApplications.Domain;

public partial class advice : System.Web.UI.Page
{
    private ILog m_Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    private PostFactory m_PostFactory;
    private IMessageService m_MessageService;
    private HttpHelper m_HttpHelper;
    private WebMailService m_WebMailService;

    protected void Page_Load(object sender, EventArgs e)
    {
        m_PostFactory = new PostFactory();
        m_MessageService = m_PostFactory.GetMessageService();
        m_HttpHelper = new HttpHelper();
        m_WebMailService = new WebMailService();

        if (!IsPostBack)
        {
        }
    }

    protected void btnSend_Click(object sender, EventArgs e)
    {
        string confirmationCode = txtConfirmationCode.Text.Trim();
        if (string.IsNullOrEmpty(confirmationCode))
        {
            ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "js", JavascriptUtil.AlertJS(MsgVO.SECURCODE_EMPTY), false);
            return;
        }

        try
        {
            if (confirmationCode.ToUpper().Equals(Session["Captcha"].ToString().ToUpper()))
            {
                MessageVO messageVO = new MessageVO();
                UIHelper.FillVO(PanelUI, messageVO);
                messageVO.CreatedDate = DateTime.Now;
                messageVO.CreateIP = m_HttpHelper.GetUserIp(Context);
                messageVO = m_MessageService.CreateMessage(messageVO);

                //發信
                m_WebMailService.SendMail_ToContactor_ByMessage(messageVO);

                ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "js", JavascriptUtil.AlertJS("問題諮詢已送出! 診所服務人員將盡速回覆您，謝謝。"), false);
                ClearUI();
            }
            else
            {
                ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "js", JavascriptUtil.AlertJS(MsgVO.SECURCODE_ERROR), false);
            }
        }
        catch (Exception ex)
        {
            ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "js", JavascriptUtil.AlertJS("送出失敗! 系統忙碌中，請重新送出諮詢問題。"), false);
        }
    }

    private void ClearUI()
    {
        UIHelper.ClearUI(PanelUI);
    }
}